/*
 * 更新系统设置
 */
package cn.edu.ntu.yyl.service.imlp;


import cn.edu.ntu.yyl.dao.OptionDao;
import cn.edu.ntu.yyl.pojo.Option;
import cn.edu.ntu.yyl.service.OptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class OptionServiceImpl implements OptionService {
	@Autowired
	private OptionDao Dao;
	
	//获取所有设置
	public Map<String, Object> getalloption(){
		Map<String, Object> optionmap;
		//取得SqlSession对象(openSession)

 		try {
	 		//getMapper

		 	//执行SQL
		 	optionmap = toMap(Dao.getoptionlist());
			return optionmap;
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
	}
	
	//更新文章设置
	public int UpdateArticleSetting(Map<String, Object> inmap){

		//取得SqlSession对象(openSession)

 		try {
	 		//getMapper

		 	//执行SQL
		 	Dao.UpdateArticleSetting(inmap);

			return 1;
        } catch (Exception e) {
			System.err.println(e.toString());
			return -1;
        }

	}
	
	//更新附件设置
	public int UpdateAttachmentSetting(Map<String, Object> inmap){

		//取得SqlSession对象(openSession)

 		try {
	 		//getMapper

		 	//执行SQL
		 	Dao.UpdateAttachmentSetting(inmap);

			return 1;
        } catch (Exception e) {
			System.err.println(e.toString());
			return -1;
        }

	}
	
	//更新系统设置
	public int UpdateSystemSetting(Map<String, Object> inmap){

		//取得SqlSession对象(openSession)

 		try {
	 		//getMapper

		 	//执行SQL
		 	Dao.UpdateSystemSetting(inmap);

		 	return 1;
        } catch (Exception e) {

			System.err.println(e.toString());
			return -1;
        }

	}
	
	//更新注册设置
	public int UpdateSystemRegSetting(Map<String, Object> inmap){
		//取得SqlSession对象(openSession)

 		try {
	 		//getMapper

		 	//执行SQL
		 	Dao.UpdateSystemRegSetting(inmap);
		 	return 1;
        } catch (Exception e) {
            System.err.println(e.toString());
			return -1;
        }
	}

	@Override
	public String AutoSelect(String key ) {
		//取得SqlSession对象(openSession)

		try {
			//getMapper

			//执行SQL
			return Dao.AutoSelect(key);
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		}
	}

	//将List转化为Map
	private Map<String, Object> toMap(List<Option> list){
		Map<String, Object> optionmap = new HashMap<>();
		for (Option option : list) {
			optionmap.put(option.getKey(), option.getValue());
		}
		return optionmap;
	}
}
